/**
 * 
 */
package com.letv.ads.view;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.letv.adlib.managers.status.ad.AdStatusManager;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.ads.util.AdsUtils;

/**
 *
 */
public class AdTextView extends TextView implements IAdView {
    private static final String TAG = "AdTextView";
    private AdElementMime mAdInfo;
    private AdViewOnclickListenr mAdViewOnclickListenr;
    private AdMaterialLoadListener mAdMaterialLoadListener;
    private AdPlayStateListener mAdPlayStateListener;
    private boolean mHasClosed = false;

    public AdTextView(Context context) {
        super(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public AdTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void showAD(AdElementMime adInfo) {
        if (null != adInfo) {
            this.mAdInfo = adInfo;
            setText(adInfo.text);
            if (null != mAdMaterialLoadListener) {
                mAdMaterialLoadListener.onLoadComplete();
            }
            if (null != mAdPlayStateListener) {
                Bundle bundle = new Bundle();
                bundle.putInt(KEY_STATE, PLAY_STATE_STARTED);
                mAdPlayStateListener.onADPlayStateChange(bundle);
            }
            // 混合非播放器内广告控制重复曝光，其他广告不控制
            if (!adInfo.hadExposed || !adInfo.isComplexAD()) {
                adInfo.hadExposed = true;
                /**
                 * 曝光统计
                 * */
                AdStatusManager statusMgr = new AdStatusManager(adInfo);
                statusMgr.onAdPlayStart();
            }
            setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (null != mAdViewOnclickListenr
                            && AdsUtils.checkClickEvent()) {
                        mAdViewOnclickListenr.onADClick(mAdInfo);
                    }
                }
            });

        }
    }

    @Override
    public void closeAD() {
        if (mHasClosed) {
            return;
        } else {
            mHasClosed = true;
        }
        setVisibility(GONE);
        if (null != mAdInfo) {
            /**
             * 完成统计
             * */
            AdStatusManager statusMgr = new AdStatusManager(mAdInfo);
            statusMgr.onAdPlayComplete();

        }
    }

    /*
     * @see com.letv.ads.view.IAdView#setAdOnClickListener(com.letv.ads.view.
     * AdViewOnclickListenr)
     */
    @Override
    public void setAdOnClickListener(
            final AdViewOnclickListenr adViewOnclickListenr) {
        mAdViewOnclickListenr = adViewOnclickListenr;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.letv.ads.view.IAdView#setAdMaterialLoadListener(com.letv.ads.view
     * .IAdView.AdMaterialLoadListener)
     */
    @Override
    public void setAdMaterialLoadListener(
            AdMaterialLoadListener adMaterialLoadListener) {
        mAdMaterialLoadListener = adMaterialLoadListener;
    }

    @Override
    public void setAdPlayStateListener(AdPlayStateListener adPlayStateListener) {
        mAdPlayStateListener = adPlayStateListener;
    }
}
